package com.example.springsecuritydemo2.domain.vo;

import com.example.springsecuritydemo2.domain.User;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Data
@NoArgsConstructor
public class LoginUser implements UserDetails {

    private User user;

    private List<String> permissions;

    // 自定义应该权限列表集合
    List<SimpleGrantedAuthority> authorities = new ArrayList<>(); // 子类

    public LoginUser(User user) {
        this.user = user;
    }

    public LoginUser(User user, List<String> permissions) {
        this.user = user;
        this.permissions = permissions;
    }

    // 用来返回权限信息
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if(!authorities.isEmpty()){
            System.out.println("authorities != null："+authorities);
            return authorities;
        }
        for(String s : permissions){
            authorities.add(new SimpleGrantedAuthority(s));
        }
        System.out.println("authorities == null："+authorities);
        return authorities;
    }

    // 返回密码
    @Override
    public String getPassword() {
        return user.getPassword();
    }

    // 返回用户名
    @Override
    public String getUsername() {
        return user.getUserName();
    }

    // 账号是否未过期
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    // 账号是否未锁定
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    // 密码是否未过期
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    // 账号是否可用
    @Override
    public boolean isEnabled() {
        return true;
    }
}
